<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Listener;

use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Consul\Agent;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Http\Server\HttpServer;
use Swoft\Log\Helper\CLog;
use Swoft\Server\SwooleEvent;

/**
 * Class RegisterServiceListener
 *
 * @since 2.0
 *
 * @Listener(event=SwooleEvent::START)
 */
class RegisterServiceListener implements EventHandlerInterface
{
    /**
     * @Inject()
     *
     * @var Agent
     */
    private $agent;

    /**
     * @param EventInterface $event
     */
    public function handle(EventInterface $event): void
    {
        /** @var HttpServer $httpServer */
        $httpServer = $event->getTarget();

//        $service = [
//            'ID'                => config('consul.SERVICE_ID'),
//            'Name'              => config('consul.SERVICE_NAME'),
//            'Tags'              => [
//                'http'
//            ],
//            'Address'           => env('HOST_IP'),
//            'Port'              => (int)config('application.HTTP_PORT'),
//            'Meta'              => [
//                'version' => '1.0'
//            ],
//            'EnableTagOverride' => false,
//            'Weights'           => [
//                'Passing' => 10,
//                'Warning' => 1
//            ],
//            "Checks" => [//http健康检测
//                [
//                    "name" => "saas-check",
//                    "http" => 'http://'.env('HOST_IP').':' . $httpServer->getPort() . '/consul/health',
//                    "interval" => "10s",
//                    "timeout" => "5s"
//                ]
//            ]
//        ];
        // Register
        //$this->agent->registerService($service);

        //注册RPC服务
        $rpcService = [
            'ID'                => config('consul.SERVICE_ID').'-rpc',
            'Name'              => config('consul.SERVICE_NAME'),
            'Tags'              => [
                'rpc'
            ],
            'Address'           => env('HOST_IP'),
            'Port'              => (int)config('application.RPC_PORT'),
            'Meta'              => [
                'version' => '1.0'
            ],
            'EnableTagOverride' => false,
            'Weights'           => [
                'Passing' => 10,
                'Warning' => 1
            ],
            "Checks" => [//http健康检测
                [
                    "name" => "saas-check",
                    "http" => 'http://'.env('HOST_IP').':' . $httpServer->getPort() . '/consul/health',
                    "interval" => "10s",
                    "timeout" => "5s"
                ]
            ]
        ];
        $this->agent->registerService($rpcService);

        CLog::info('Swoft http register service success by consul!');
    }
}